package com.affectu.user.action;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.security.Identity;

import com.affectu.common.Messages;
import com.affectu.dao.DAOBase;
import com.affectu.user.User;

/*
 * 2008-8-10 
 * @author daniel nathan
 */
@Name("userAction")
public class UserAction extends DAOBase {

	@In(required = false)
	private User user;
	public boolean isLogin;
	public Messages messages = new Messages();

	public UserAction() {
		this.getMessages().setMessage1("账户激活邮件将发送至该邮箱.");
		this.getMessages().setMessage2("密码由6-16个字母或数字组成.");
		this.getMessages().setMessage3("再输入一遍您的密码以确保没有输错.");
		this.getMessages().setMessage4("您在affectu平台显示的名字，由3-20个字母或数字组成.");
		this.getMessages().setMessage5("←点击图片换一个验证码.");
	}

	public void add() {
		em.persist(user);
		em.flush();
		this.log(user.getEmail());
		this.getSessionScope().put("loginUser", user);
		// this.gotoPage("/default.xhtml");//跨文件夹跳转，在pages.xml里配置
	}

	public boolean checkInput(User user) {

		if (user.getEmail().indexOf("@") == -1) {
			this.getMessages().setMessage1(this.addRedColor("请正确输入Email地址."));
			return false;
		} else if (this.checkEmail(user.getEmail())) {
			this.getMessages().setMessage1(
					this.addRedColor("该Email已被注册，请重新输入."));
			return false;
		}
		if (user.getPassword().length() < 6) {
			this.getMessages().setMessage2(this.addRedColor("该密码小于6位，请重新输入."));
			return false;
		} else if (user.getPassword().length() > 16) {
			this.getMessages()
					.setMessage2(this.addRedColor("该密码已超过16位，请重新输入."));
			return false;
		}
		if (!user.getPassword().equals(user.getPasswordConfirm())) {
			this.getMessages().setMessage3(this.addRedColor("两次密码输入不一致."));
			return false;
		}
		if (!this.checkCode(user.getCheckCode())) {
			this.getMessages().setMessage5(this.addRedColor("验证码不正确，请重新输入."));
			return false;
		}
		return true;
	}

	@SuppressWarnings("deprecation")
	public String register(User user) {
		String url = "";
		if (this.checkInput(user)) {
			em.persist(user);
			Identity.instance().setUsername(user.getEmail());
			Identity.instance().setPassword(user.getPassword());
			Identity.instance().login();
			this.setCurrentUserToSession(user);
			url = "/index.xhtml";
		}
		return url;
	}

	/**
	 * @return the messages
	 */
	public Messages getMessages() {
		return messages;
	}

	/**
	 * @param messages
	 *            the messages to set
	 */
	public void setMessages(Messages messages) {
		this.messages = messages;
	}

	private boolean checkEmail(String email) {
		boolean flag = false;
		try {
			Query query = em.createQuery("from User u where u.email=:email")
					.setString("email", email);
			if (query.list().size() > 0) {
				flag = true;
			}
		} catch (Exception ex) {
			this.logException(ex);
		}
		return flag;

	}

	public boolean checkCode(String checkCode) {
		return (checkCode.equals(this.getSessionScope().get("checkCode")));
	}

	@SuppressWarnings("unchecked")
	@Factory(value = "userAction_users", scope = ScopeType.PAGE)
	public List<User> getUsers() {
		List<User> users = new ArrayList<User>();
		try {
			users = em.createQuery("select u from User u order by u.id desc")
					.list();
		} catch (Exception ex) {
			this.logException(ex);
		}
		return users;
	}
}
